/**
 * 生成 CRUD 操作相关状态
 *
 * @template T 表单类型
 * @param {() => T} formDefault 获取表单默认值
 * @param {StoreType} store 数据存储仓库
 * @param {ReturnType<typeof genDelConfHandler>} genDelConfs 生成删除操作
 * @returns {[
 *   Ref<T, T>,
 *   Ref<boolean, boolean>,
 *   Ref<boolean, boolean>,
 *   () => void,
 *   (id: string) => void,
 *   ReturnType<typeof genDelConfsHandler>
 * ]} 返回一个数组：
 * - 表单状态
 * - 是否展示表单编辑器
 * - 是否为新增表单
 * - 新增表单处理函数
 * - 编辑表单处理函数
 * - 删除操作处理函数
 */
export const genCrudStatus = <T>(
  formDefault: () => T,
  store: StoreType,
  genDelConfs: () => ReturnType<typeof genDelConfsHandler>
): {
  form: Ref<T, T>
  isShowEditor: Ref<boolean, boolean>
  isAdd: Ref<boolean, boolean>
  addConf: () => void
  editConf: (id: string) => void
  delConfs: ReturnType<typeof genDelConfsHandler>
} => {
  const form = ref<T>(formDefault()) as Ref<T, T>
  const isShowEditor = ref(false)
  const isAdd = ref(false)
  const addConf = () => {
    form.value = formDefault()
    isAdd.value = true
    isShowEditor.value = true
  }
  const editConf = (id: string) => {
    form.value = store.getMap()[id]
    isAdd.value = false
    isShowEditor.value = true
  }
  const delConfs = genDelConfs()

  return { form, isShowEditor, isAdd, addConf, editConf, delConfs }
}
